package com.jackrain.nea.fc.vp.mapper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * FC-会员接口-VIP升降级-FC新增/修改 —> F4
 * Mapper
 *
 * @author WSL
 * @create 2017/11/17
 */
@Mapper
public interface ViewCvipUpMapper {

    class SelectPro
    {
        public String selectProvider(JSONObject param)
        {
            String sql = new SQL()
            {
                {
                    SELECT("u.REMARK,u.VP_C_VIP_ID,u.VP_C_VIP_ACC_ID,u.VP_C_VIPTYPE_OLDID,u.VP_C_VIPTYPE_NEWID,u.DATEUP,u.OWNERENAME,u.TYPE");
                    FROM("VP_C_VIP_UP u");
                    INNER_JOIN("VP_C_VIPTYPEGROUP t ON t.ID = u.VP_C_VIPTYPEGROUP_ID");
                    WHERE("u.ISREAD = 'N'");
                    AND();
                    WHERE("t.ECODE = 'FF'");
                    AND();
                    WHERE("u.MODIFIEDDATE BETWEEN #{startDate} AND #{endDate}");
                }
            } .toString();

            System.out.println("sql =====>"+sql);
            return sql;
        }
    }
    /**
     * 根据param修改时间查询条件 查询【VIP升级日志】表
     * @return
     */
    @SelectProvider(type = SelectPro.class, method = "selectProvider")
    List<HashMap> selectProvider(JSONObject param);

    class SelectProByRow
    {
        public String selectProviderByRow(JSONObject param)
        {
            String sql = new SQL()
            {
                {
                    SELECT("u.ID,u.REMARK,u.VP_C_VIP_ID,u.VP_C_VIP_ACC_ID,u.VP_C_VIPTYPE_OLDID,u.VP_C_VIPTYPE_NEWID,u.DATEUP,u.OWNERENAME,u.TYPE");
                    FROM("VP_C_VIP_UP u");
                    INNER_JOIN("VP_C_VIPTYPEGROUP t ON t.ID = u.VP_C_VIPTYPEGROUP_ID");
                    WHERE("u.ISREAD = 'N'");
                    AND();
                    WHERE("t.ECODE = 'FF'");
                    ORDER_BY("u.MODIFIEDDATE ASC LIMIT 0,#{row}");
                }
            } .toString();

            System.out.println("sql =====>"+sql);
            return sql;
        }
    }
    /**
     * 根据param修改时间查询条件 查询【VIP升级日志】表
     * @return
     */
    @SelectProvider(type = SelectProByRow.class, method = "selectProviderByRow")
    List<HashMap> selectProviderByRow(JSONObject param);

    /**
     * 根据表【VP_C_VIP_UP】中字段【VP_C_VIP_ACC_ID】查询表【VP_C_VIP_ACC】中的ECODE的值
     * @param id
     * @param vipId
     * @return
     */
    @Select("SELECT ECODE FROM VP_C_VIP_ACC WHERE ID = #{arg0} AND VP_C_VIP_ID = #{arg1}")
    String getVipAccEcode(Long id,Long vipId);

    /**
     * 外键关联表【VP_C_VIPTYPE】取值字段：ECODE
     * @param id
     * @return
     */
    @Select("SELECT ECODE FROM VP_C_VIPTYPE WHERE ID = #{id}")
    String getVipTypeEcode(Long id);

    class CvipUpUpdateSql
    {
        public String updateCvipUp(final JSONObject param)
        {
            String sql = new SQL()
            {
                {
                    JSONArray ids = param.getJSONArray("IDS");
                    UPDATE("VP_C_VIP_UP");
                    SET("ISREAD = 'Y'");
                    StringBuilder sb = new StringBuilder("(");
                    for (int i = 0; i < ids.size(); i++) {
                        sb.append(ids.getIntValue(i)+",");
                    }
                    sb.deleteCharAt(sb.length()-1).append(")");
                    WHERE("ID IN "+sb.toString());
                }
            } .toString();
            System.out.println("sql =====>"+sql);
            return sql;
        }
    }

    /**
     * 更新VP_C_VIP_UP表中ISREAD字段
     * @param param
     */
    @UpdateProvider(type = CvipUpUpdateSql.class,
            method = "updateCvipUp")
    int updateIsRead(JSONObject param);

}
